<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="查询日期" style="width: 328px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="车间" prop="workshopName">
          <el-select
            style="width: 180px"
            @change="handleQuery"
            v-model="queryParams.workshopName"
          >
            <el-option label="一车间" value="一车间"></el-option>
            <el-option label="二车间" value="二车间"></el-option>
            <el-option label="三车间" value="三车间"></el-option>
            <el-option label="四车间" value="四车间"></el-option>
            <el-option label="五车间" value="五车间"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机台名称" prop="machineName">
          <!-- <el-input
            v-model.trim="queryParams.machineName"
            style="width: 180px"
            placeholder="请输入机台"
            @keyup.enter="handleQuery"
          /> -->
          <el-select
            filterable
            @change="handleQuery"
            v-model="queryParams.machineName"
          >
            <el-option
              v-for="item in workbranchList"
              :key="item"
              :label="item.machineName"
              :value="item.machineName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="纸张名称" prop="name">
          <el-input
            v-model.trim="queryParams.name"
            style="width: 180px"
            placeholder="请输入纸张名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="纸张编码" prop="code">
          <el-input
            v-model.trim="queryParams.code"
            style="width: 180px"
            placeholder="请输入纸张编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="定量" prop="weight">
          <el-input
            type="number"
            v-model="queryParams.weight"
            placeholder="请输入定量"
            clearable
            style="width: 180px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="specs">
          <el-select
            filterable
            clearable
            @change="handleQuery"
            @keyup.enter="handleQuery"
            v-model="queryParams.specs"
            class="m-2"
            placeholder="请选择"
            style="width: 180px"
          >
            <el-option
              v-for="item in mes_paper_specs"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="幅宽" prop="longWide">
          <el-input
            v-model.trim="queryParams.longWide"
            clearable
            style="width: 180px"
            placeholder="请输入幅宽"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-select
            clearable
            style="width: 180px"
            @change="handleQuery"
            v-model="queryParams.brand"
            class="m-2"
            filterable
            placeholder="请选择纸张品牌"
          >
            <el-option
              v-for="item in mes_paper_brand"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-col :span="1.5">
      <el-button type="primary" plain icon="Download" @click="handleExport"
        >导出</el-button
      >
    </el-col>

    <div>
      <el-table
        height="65vh"
        @selection-change="handleSelectionChange"
        v-loading="loading"
        border
        :data="dataList"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="车间"
          min-width="80"
          align="center"
          :show-overflow-tooltip="true"
          prop="workshopName"
          v-if="columns[0].visible"
        >
        </el-table-column>
        <el-table-column
          label="机台名称"
          min-width="120"
          align="center"
          prop="machineName"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="机长"
          min-width="100"
          align="center"
          prop="userName"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="纸张编码"
          v-if="columns[5].visible"
          width="180"
          align="center"
          prop="code"
        ></el-table-column>
        <el-table-column
          label="纸张名称"
          min-width="160"
          align="center"
          prop="name"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="规格"
          width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="specs"
        >
          <template #default="scope">
            <span v-if="scope.row.longWide">
              {{ scope.row.longWide }}
            </span>
            <span v-if="scope.row.specs">
              <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
            </span>
          </template>
          <!-- <template #default="scope">
            <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
          </template> -->
        </el-table-column>
        <el-table-column
          label="定量"
          v-if="columns[6].visible"
          width="70"
          align="center"
          prop="weight"
        ></el-table-column>
        <el-table-column
          label="期初库存"
          min-width="110"
          align="center"
          inputmode="numeric"
          prop="realityInventoryQC"
          v-if="columns[1].visible"
        ></el-table-column>
        <el-table-column
          label="累计领用数量"
          v-if="columns[7].visible"
          width="130"
          align="center"
          prop="useNumber"
        >
          <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewUseNumber(scope.row)"
              text
            >
              {{ scope.row.useNumber }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="累计退库数量"
          v-if="columns[7].visible"
          width="130"
          align="center"
          prop="tkNumber"
        >
          <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewTKNumber(scope.row)"
              text
            >
              {{ scope.row.tkNumber }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="内部调入量"
          v-if="columns[7].visible"
          width="110"
          align="center"
          prop="realityEntryInventory"
        ></el-table-column>
        <el-table-column
          label="累计报工量"
          v-if="columns[7].visible"
          width="110"
          align="center"
          prop="realityLing"
        >
          <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewRealityLing(scope.row)"
              text
            >
              {{ scope.row.realityLing }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="内部调出量"
          v-if="columns[7].visible"
          width="110"
          align="center"
          prop="realityOutInventory"
        ></el-table-column>
        <el-table-column
          label="盈亏"
          min-width="120"
          align="center"
          inputmode="numeric"
          prop="ykNumber"
          v-if="columns[1].visible"
        >
          <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewYKNumber(scope.row)"
              text
            >
              {{ scope.row.ykNumber }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="实时库存"
          min-width="120"
          align="center"
          inputmode="numeric"
          prop="realityInventory"
          v-if="columns[1].visible"
        ></el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>
  
  <script setup name="SearchMachineInventory">
import { getMachineInventoryList } from "@/api/production/cover.js";
import { getList as getWorkBenchList } from "@/api/basicInfo/workbench";
const workbranchList = ref([]);
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const sheetTotal = ref(0);
const platemakingTotal = ref(0);
const multiple = ref(true);
const selectRow = ref([]);
const printOpen = ref(false);
const printList = ref([]);
const realityInventory = ref(0);
// 列显隐信息
const columns = ref([
  { key: 0, label: `车间`, visible: true },
  { key: 1, label: `机台名称`, visible: true },
  { key: 2, label: `机长`, visible: true },
  { key: 3, label: `纸张编码`, visible: true },
  { key: 4, label: `纸张名称`, visible: true },
  { key: 5, label: `定量`, visible: true },
  { key: 6, label: `累计领用数量`, visible: true },
  { key: 7, label: `累计报工量`, visible: true },
  { key: 8, label: `实时库存`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
  },
});

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/produce/export",
    {
      ...queryParams.value,
    },
    `机台库存查询.xlsx`
  );
}

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "realityInventory":
        sums[index] = realityInventory.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
const getDateList = () => {
  getWorkBenchList({ pageNum: 1, pageSize: 100, type: "2" }).then((resp) => {
    workbranchList.value = resp.rows;
  });
};

getDateList();
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);

const handleSelectionChange = (row) => {
  selectRow.value = row;
  multiple.value = !row.length;
};

const { queryParams } = toRefs(data);
const {
  ems_machine_info_type,
  ems_machine_info_model,
  ems_order_status,
  mes_paper_specs,
  mes_paper_brand,
} = proxy.useDict(
  "ems_machine_info_type",
  "ems_order_status",
  "ems_machine_info_model",
  "mes_paper_specs",
  "mes_paper_brand"
);

const getList = () => {
  loading.value = true;
  queryParams.value.startDate = Array.isArray(dateRange.value)
    ? dateRange.value[0]
    : undefined;
  queryParams.value.endDate =
    Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  getMachineInventoryList({ ...queryParams.value })
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      realityInventory.value = res.rows[0]?.hashMap?.realityInventory;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

const handleViewCode = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.platemakingId,
      info: true,
    },
  });
};

//累计领用数量穿透
const handleViewUseNumber = (row) => {
  router.push({
    path: "/production/searchMachineInventory/accessDetail",
    query: {
      paperId: row.paperId,
      machineId: row.machineId,
      startDate: queryParams.value.startDate,
      endDate: queryParams.value.endDate,
      info: true,
    },
  });
};

//盈亏：盘点明细数据穿透
const handleViewYKNumber = (row) => {
  router.push({
    path: "/production/searchMachineInventory/inventorySheetDetail",
    query: {
      paperId: row.paperId,
      machineId: row.machineId,
      startDate: queryParams.value.startDate,
      endDate: queryParams.value.endDate,
      info: true,
    },
  });
};

//累计退库数量穿透
const handleViewTKNumber = (row) => {
  router.push({
    path: "/production/searchMachineInventory/cancellationDetail",
    query: {
      paperId: row.paperId,
      machineId: row.machineId,
      startDate: queryParams.value.startDate,
      endDate: queryParams.value.endDate,
      info: true,
    },
  });
};

//累计报工量穿透
const handleViewRealityLing = (row) => {
  router.push({
    path: "/production/searchMachineInventory/reportDetail",
    query: {
      paperId: row.paperId,
      machineId: row.machineId,
      startDate: queryParams.value.startDate,
      endDate: queryParams.value.endDate,
      info: true,
    },
  });
};

getList();
</script>
  
  <style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>
  